package com.myself.ssoserver.service

import com.myself.common.service.BaseService
import com.myself.ssoserver.entity.Nomination
import com.myself.ssoserver.entity.Nominations
import com.myself.ssoserver.repository.NominationRepository
import org.ktorm.dsl.and
import org.ktorm.dsl.eq
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
open class NominationService(
    private val repository: NominationRepository
) : BaseService<Nomination, Nominations>(repository) {

    @Transactional
    open fun updateOrSave(nomination: Nomination) {
        val find =repository.findOneBy { (it.userId eq nomination.user.id) and (it.versionId eq nomination.version.id) }
        if (find == null) {
            repository.addOne(nomination)
        }else{
            nomination.id = find.id
            repository.updateOne(nomination)
        }
    }
}